/*
 * Copyright (c) 2010 Toni Spets <toni.spets@iki.fi>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "common.h"
#include "config.h"

#define INI_NAME "cncnet.ini"

char        config_server[64] = "lobby.cncnet.cnc-comm.com";
uint16_t    config_server_port = NET_PORT;
uint16_t    config_port = NET_PORT;
uint8_t     config_port_force = FALSE;
uint8_t     config_upnp = TRUE;
uint16_t    config_upnp_discover = 2000;

void config_init()
{
    char cwd[MAX_PATH];
    char ini_path[MAX_PATH];
    GetCurrentDirectoryA(sizeof(cwd), cwd);
    snprintf(ini_path, sizeof(ini_path), "%s\\%s", cwd, INI_NAME);

    if(GetFileAttributes(ini_path) == 0xFFFFFFFF)
    {
        FILE *fh = fopen(ini_path, "w");
        fprintf(fh,
            "[CnCNet]\n"
            "; lobby server address\n"
            "Server=%s\n"
            "; lobby server port\n"
            "ServerPort=%d\n"
            "; client port (our)\n"
            "Port=%d\n"
            "; lie about our port to workaround some broken routers\n"
            "PortForce=%s\n"
            "; enable UPnP autoconfiguration\n"
            "UPnP=%s\n"
            "; UPnP discover time in milliseconds\n"
            "UPnPDiscover=%d\n"
        , config_server, config_server_port, config_port, config_port_force ? "yes" : "no", config_upnp ? "yes" : "no", config_upnp_discover);
        fclose(fh);
    }

    char tmp[2] = { 0x00, 0x00 };
    uint16_t itmp;
    GetPrivateProfileStringA("CnCNet", "Server", config_server, config_server, sizeof(config_server), ini_path);

    itmp = GetPrivateProfileIntA("CnCNet", "ServerPort", config_server_port, ini_path);
    if(itmp > 1024 && itmp < 65535)
    {
        config_server_port = (uint16_t)itmp;
    }

    itmp = GetPrivateProfileIntA("CnCNet", "Port", config_port, ini_path);
    if(itmp > 1024 && itmp < 65535)
    {
        config_port = (uint16_t)itmp;
    }

    GetPrivateProfileStringA("CnCNet", "PortForce", "n", tmp, sizeof(tmp), ini_path);
    if(tolower(tmp[0]) == 'y' || tolower(tmp[0]) == 'e'|| tolower(tmp[0]) == 't' || tmp[0] == '1')
    {
        config_port_force = TRUE;
    }
    else 
    {
        config_port_force = FALSE;
    }

    GetPrivateProfileStringA("CnCNet", "UPnP", "y", tmp, sizeof(tmp), ini_path);
    if(tolower(tmp[0]) == 'y' || tolower(tmp[0]) == 'e'|| tolower(tmp[0]) == 't' || tmp[0] == '1')
    {
        config_upnp = TRUE;
    }
    else 
    {
        config_upnp = FALSE;
    }

    itmp = GetPrivateProfileIntA("CnCNet", "UPnPDiscover", config_upnp_discover, ini_path);
    if(itmp > 0 && itmp < 60000)
    {
        config_upnp_discover = (uint16_t)itmp;
    }
}
